﻿using LongGi.TingkatKebocoranModule.CommModel;
using LPLS.DBEntities.LongGIModule;
using NPOI.SS.UserModel;
using NPOI.Util;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using LineStyle = OxyPlot.LineStyle;

namespace LongGi.TingkatKebocoranModule.ViewModels
{
    /// <summary>
    /// 卡浆
    /// </summary>
    public class PaddleCheckViewModel : MyViewModelBase
    {


        public PaddleCheckViewModel()
        {

           
            LongGiEventCommon.KajiangDataEvent += LongGiEventCommon_KajiangDataEvent;
            LongGiEventCommon.ExportExcelDataEvent += LongGiEventCommon_ExportExcelDataEvent;
            JinZhouFlag = false;
            ChuZhouFlag = false;

        }


        List<LPLS.DBEntities.LongGIModule.KaJiangData> ExportData = new List<LPLS.DBEntities.LongGIModule.KaJiangData>();
        /// <summary>
        /// 数据导出 事件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void LongGiEventCommon_ExportExcelDataEvent()
        {
            ExportFileCommon.ExportFile<KaJiangData>("扭矩数据",ExportData);
        }

        private void LongGiEventCommon_KajiangDataEvent(List<LPLS.DBEntities.LongGIModule.KaJiangData> obj)
        {
            ExportData.Clear();           
            var li = obj.OrderBy(e => e.Time).Where(e => e != null).ToList();
            ExportData = li;
            GetData(li);
        }
        //LP只有负载 暂时不做
        //pengkuo 
        private List<LPLS.DBEntities.LongGIModule.KaJiangData> catchData = new List<LPLS.DBEntities.LongGIModule.KaJiangData>();
        /// <summary>
        /// 进舟 负载 平均值
        /// </summary>
        private List<MyPoint> jinzhouFuzaiAgv = new List<MyPoint>();
        /// <summary>
        /// 进舟负载 最大值集合
        /// </summary>
        private List<MyPoint> jinzhouFuzaiMax = new List<MyPoint>();

        /// <summary>
        /// 进舟空载 最大值集合
        /// </summary>
        private List<MyPoint> jinzhouKongzaiMax = new List<MyPoint>();
        /// <summary>
        /// 进舟 空载 平均值
        /// </summary>
        private List<MyPoint> jinzhouKongzaiAgv = new List<MyPoint>();
        /// <summary>
        /// 出舟 负载  平均值
        /// </summary>
        private List<MyPoint> chuzhouFuzaiAgv = new List<MyPoint>();
        /// <summary>
        /// 出舟负载 最大值集合
        /// </summary>
        private List<MyPoint> chuzhouFuzaiMax = new List<MyPoint>();
        /// <summary>
        /// 出舟 空载 最大值
        /// </summary>
        private List<MyPoint> chuzhouKongzaiMax = new List<MyPoint>();
        /// <summary>
        /// 出舟  空载  平均值
        /// </summary>
        private List<MyPoint> chuzhouKongzaiAgv = new List<MyPoint>();

        private void GetData(List<LPLS.DBEntities.LongGIModule.KaJiangData> list)
        {
            jinzhouFuzaiMax.Clear();
            jinzhouKongzaiMax.Clear();
            jinzhouFuzaiAgv.Clear();
            jinzhouKongzaiAgv.Clear();
            chuzhouFuzaiAgv.Clear();
            chuzhouFuzaiMax.Clear();
            chuzhouKongzaiMax.Clear();
            chuzhouKongzaiAgv.Clear();
            jinzhouFuzaiAgv.Clear();
            catchData.Clear();
            catchData = list;

            foreach (var item in catchData)
            {

                if (item.StepName == "进舟")
                {
                    #region  进舟
                    /// 进舟负载         
                    if (item.TypeNumber==1)
                    {
                       

                        if (item.TypeValue==1)
                        {
                            jinzhouFuzaiMax.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                        else
                        {
                            jinzhouFuzaiAgv.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                    }
                       

                    else
                    {

                        if (item.TypeValue == 1)
                        {
                            jinzhouKongzaiMax.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                        else
                        {
                            jinzhouKongzaiAgv.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                      
                     

                    }
                    //进舟 空载
                 
                    #endregion

                }
                else//出舟
                {
                    #region 出舟

                    //负载
                    if (item.TypeNumber==1)
                    {
                        if (item.TypeValue==1)
                        {
                            chuzhouFuzaiMax.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                        else//平均值
                        {
                            chuzhouFuzaiAgv.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                      
                    }

                    else//空载
                    {

                        if (item.TypeValue==1)
                        {
                            chuzhouKongzaiMax.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) }); }
                        else
                        {
                            chuzhouKongzaiAgv.Add(new MyPoint { Time = item.Time, Value = Math.Round((double)item.Value, 2) });
                        }
                       
                    }
                 


                    #endregion
                }

            }
            JinZhouFlag = true;
            ChuZhouFlag = false;
        }



        private bool _ChuZhouFlag;
        public bool ChuZhouFlag
        {
            get { return _ChuZhouFlag; }
            set
            {

                SetProperty(ref _ChuZhouFlag, value);
                
                LeakRatePlotModel = QuaryChart();
                var top = this.TopValue;

                this.TopValue = top;

                var bottom = this.BottomValue;
                this.BottomValue = bottom;


            }
        }

        private bool _JinZhouFlag;
        public bool JinZhouFlag
        {
            get { return _JinZhouFlag; }
            set
            {
                SetProperty(ref _JinZhouFlag, value);
                LeakRatePlotModel = QuaryChart();
                var top = this.TopValue;

                this.TopValue = top;

                var bottom = this.BottomValue;
                this.BottomValue = bottom;

            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="data"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        private PlotModel QuaryChart()
        {
            var series = new List<LineSeries>();
            var model = new PlotModel();
            //出舟被选中了则加载出舟的数据
            if (ChuZhouFlag)
            {
                var chuzhouFuZaiAvgseries = new LineSeries
                {
                    Title = "出舟负载-平均值",
                    ItemsSource = chuzhouFuzaiAgv,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhouFuZaiAvgseries);

                var chuzhouFuzaiMaxSer = new LineSeries
                {
                    Title = "出舟负载-最大值",
                    ItemsSource = chuzhouFuzaiMax,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhouFuzaiMaxSer);
                var chuzhoukongZaiAvgseries = new LineSeries
                {
                    Title = "出舟空载-平均值",
                    ItemsSource = chuzhouKongzaiAgv,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhoukongZaiAvgseries);
                var chuzhoukongzaiMaxSer = new LineSeries
                {
                    Title = "出舟空载-最大值",
                    ItemsSource = chuzhouKongzaiMax,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhoukongzaiMaxSer);

            }
            if (JinZhouFlag)
            {

                var chuzhouFuZaiAvgseries = new LineSeries
                {
                    Title = "进舟负载-平均值",
                    ItemsSource = jinzhouFuzaiAgv,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhouFuZaiAvgseries);

                var chuzhouFuzaiMaxSer = new LineSeries
                {
                    Title = "进舟负载-最大值",
                    ItemsSource = jinzhouFuzaiMax,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhouFuzaiMaxSer);
                var chuzhoukongZaiAvgseries = new LineSeries
                {
                    Title = "进舟空载-平均值",
                    ItemsSource = jinzhouKongzaiAgv,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhoukongZaiAvgseries);
                var chuzhoukongzaiMaxSer = new LineSeries
                {
                    Title = "进舟空载-最大值",
                    ItemsSource = jinzhouKongzaiMax,
                    DataFieldX = "Time",
                    DataFieldY = "Value",
                    MarkerType = MarkerType.Circle,
                    TrackerFormatString = "曲线：{0}\n时间：{2:yyyy-MM-dd HH:mm:ss}\n值：{4:0.00}",

                };
                series.Add(chuzhoukongzaiMaxSer);




            }



            foreach (var item in series)
            {
                model.Series.Add(item);
            }
           
            var dateTimeAxis = new DateTimeAxis
            {
                Position = AxisPosition.Bottom,
                IntervalType = DateTimeIntervalType.Auto,
            };
            model.Axes.Add(dateTimeAxis);
            var valueAxis = new LinearAxis
            {
                Position = AxisPosition.Left,
                Minimum = -0.5,
                Maximum = 40,
                Title = "值",
                  IsPanEnabled = false,
                IsZoomEnabled = false

            };
            model.Axes.Add(valueAxis);
            Legend l = new Legend();
            l = LongGiCommon.GetLegend();
            model.Legends.Add(l);
            return model;

        }
    }
}
